package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.web;


import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.iwhalecloud.citybrain.flow.platform.manager.common.ResponseResult;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FileUploadLogVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FileUploadLogRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.common.OssProperties;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.utils.OSS;
import org.flowable.engine.common.impl.cfg.IdGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

/**
 * 文件上传接口
 *
 * @author YangYing
 * @date 2018/9/22 14:20
 */
@Controller
@RequestMapping(value = "/file")
@EnableConfigurationProperties(OssProperties.class)
public class FilesUploadController {

    private static Logger LOGGER = LoggerFactory.getLogger(FilesUploadController.class);

    @Autowired
    private FileUploadLogRepository fileUploadLogRepository;

    @Autowired
    private IdGenerator idGenerator;

    @Autowired
    private OssProperties ossProperties;

    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResponseResult uploadFiles(HttpServletRequest httpServletRequest) throws Exception{
        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) httpServletRequest;
        List<MultipartFile> fileList;
        String file = "file";
        fileList = multipartHttpServletRequest.getFiles(file);
        if (fileList == null || fileList.size() <= 0) {
            LOGGER.error("上传的文件为空");
            throw new Exception("上传的文件为空");
        }
        OSS.setup(ossProperties.getEndpoint(), ossProperties.getAccessKey(), ossProperties.getSecretKey(), ossProperties.getBucketName());
        String fileName = OSS.uploadFiles2OSS(fileList);
        String url = OSS.getUrl(fileName);

        //插入文件上传日志
        FileUploadLogVo fileUploadLog = new FileUploadLogVo();
        fileUploadLog.setId(idGenerator.getNextId());
        fileUploadLog.setFileName(fileName);
        fileUploadLog.setCreateTime(new Date());
        fileUploadLog.setUpdateTime(new Date());
        fileUploadLog.setUrl(url);

        fileUploadLogRepository.insertFileUploadLog(fileUploadLog);
        return ResponseResult.success(fileName);
    }

    @ResponseBody
    @RequestMapping(value = "/download", method = RequestMethod.GET)
    public ResponseResult downloadFiles(String objectName) throws Exception{
        OSS.setup(ossProperties.getEndpoint(), ossProperties.getAccessKey(), ossProperties.getSecretKey(), ossProperties.getBucketName());
        OSS.downloadFile(objectName);
        return null;
    }

}

